//
//  PersonalCellNode.m
//  EducationLive
//
//  Created by Apple on 2017/10/18.
//  Copyright © 2017年 ZhouHao. All rights reserved.
//

#import "PersonalCellNode.h"
#import "LoginVC.h"

@interface PersonalImageCellNode ()
// UI
@property (nonatomic, strong) ASTextNode *titleNode;
@property (nonatomic, strong) ASButtonNode *iconNode;
@property (nonatomic, strong) ASImageNode *arrowNode;
@property (nonatomic, strong) ASImageNode *lineNode;
// Data
@property (nonatomic, strong) PersonalModel *item;
@property (nonatomic, copy) NSString *title;
@end

@implementation PersonalImageCellNode

- (instancetype)initWithItem:(PersonalModel *)item title:(NSString *)title
{
    if (self = [super init]) {
        _item = item;
        _title = title;
        
        self.backgroundColor = [UIColor whiteColor];
        
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        [self addTitleNode];

        [self addIconNode];

        [self addArrowNode];

        [self addLineNode];
    }
    return self;
}

- (void)addTitleNode
{
    ASTextNode *titleNode = [[ASTextNode alloc]init];
    titleNode.placeholderEnabled = YES;
    titleNode.placeholderColor = RGB_255(245, 245, 245);
    titleNode.layerBacked = YES;
    titleNode.maximumNumberOfLines = 1;
    NSDictionary *attrs = @{ NSFontAttributeName: [UIFont fontWithName:@"HelveticaNeue" size:15.0f] ,NSForegroundColorAttributeName: RGB_255(51, 51, 51)};
    titleNode.attributedText = [[NSAttributedString alloc]initWithString:_title attributes:attrs];
    [self addSubnode:titleNode];
    _titleNode = titleNode;
}

- (void)addIconNode
{
    ASButtonNode *iconNode = [[ASButtonNode alloc]init];
    //圆角
//    iconNode.layer.cornerRadius = 30;
//    iconNode.clipsToBounds = YES;
    iconNode.backgroundColor = [UIColor clearColor];
    [iconNode setBackgroundImage:[UIImage imageNamed:@"timg"] forState:ASControlStateNormal];
    [iconNode addTarget:self action:@selector(iconNodeAction:) forControlEvents:ASControlNodeEventTouchUpInside];
    [self addSubnode:iconNode];
    _iconNode = iconNode;
}

- (void)addArrowNode
{
    ASImageNode *arrowNode = [[ASImageNode alloc]init];
    arrowNode.layerBacked = YES;
    arrowNode.backgroundColor = [UIColor clearColor];
    arrowNode.image = [UIImage imageNamed:@"xiala"];
    [self addSubnode:arrowNode];
    _arrowNode = arrowNode;
}

- (void)addLineNode
{
    ASImageNode *lineNode = [[ASImageNode alloc]init];
    lineNode.layerBacked = YES;
    lineNode.backgroundColor = RGB_255(230,230,230);
    [self addSubnode:lineNode];
    _lineNode = lineNode;
}

#pragma mark - Action
- (void)iconNodeAction:(ASButtonNode *)node
{
    _initClickedPersonalImageCellNodeBlock(self, node);
}

- (ASLayoutSpec *)layoutSpecThatFits:(ASSizeRange)constrainedSize
{
    _arrowNode.style.preferredSize = CGSizeMake(9, 15);
    _iconNode.style.preferredSize = CGSizeMake(55, 55);
    _lineNode.style.preferredSize = CGSizeMake(constrainedSize.max.width, 0.5);
    ASStackLayoutSpec *iconLayout = [ASStackLayoutSpec stackLayoutSpecWithDirection:ASStackLayoutDirectionHorizontal spacing:15 justifyContent:ASStackLayoutJustifyContentStart alignItems:ASStackLayoutAlignItemsCenter children:@[_iconNode,_arrowNode]];
    ASStackLayoutSpec *titleLayout = [ASStackLayoutSpec stackLayoutSpecWithDirection:ASStackLayoutDirectionHorizontal spacing:10 justifyContent:ASStackLayoutJustifyContentSpaceBetween alignItems:ASStackLayoutAlignItemsCenter children:@[_titleNode,iconLayout]];
    titleLayout.style.flexGrow = YES;
    
    ASInsetLayoutSpec *insetLayout = [ASInsetLayoutSpec insetLayoutSpecWithInsets:UIEdgeInsetsMake(15, 10, 15, 10) child:titleLayout];
    
    ASStackLayoutSpec *lineLayout = [ASStackLayoutSpec stackLayoutSpecWithDirection:ASStackLayoutDirectionVertical spacing:0 justifyContent:ASStackLayoutJustifyContentStart alignItems:ASStackLayoutAlignItemsStretch children:@[insetLayout,_lineNode]];
    
    return lineLayout;
}

@end


@interface PersonalCellNode ()
// UI
@property (nonatomic, strong) ASTextNode *titleNode;
@property (nonatomic, strong) ASTextNode *subtitleNode;
@property (nonatomic, strong) ASImageNode *arrowNode;
@property (nonatomic, strong) ASImageNode *lineNode;
// Data
@property (nonatomic, strong) PersonalModel *item;
@property (nonatomic, copy) NSString *title;

@end

@implementation PersonalCellNode

- (instancetype)initWithItem:(PersonalModel *)item title:(NSString *)title
{
    if (self = [super init]) {
        _item = item;
        _title = title;
        
        self.backgroundColor = [UIColor whiteColor];
        
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        [self addTitleNode];

        [self addSubtitleNode];

        [self addArrowNode];

        [self addLineNode];
    }
    return self;
}

- (void)addTitleNode
{
    ASTextNode *titleNode = [[ASTextNode alloc]init];
    titleNode.placeholderEnabled = YES;
    titleNode.placeholderColor = RGB_255(245, 245, 245);
    titleNode.layerBacked = YES;
    titleNode.maximumNumberOfLines = 1;
    NSDictionary *attrs = @{ NSFontAttributeName: [UIFont fontWithName:@"HelveticaNeue" size:15.0f] ,NSForegroundColorAttributeName: RGB_255(51, 51, 51)};
    titleNode.attributedText = [[NSAttributedString alloc]initWithString:_title attributes:attrs];
    [self addSubnode:titleNode];
    _titleNode = titleNode;
}

- (void)addSubtitleNode
{
    ASTextNode *subtitleNode = [[ASTextNode alloc]init];
    subtitleNode.placeholderEnabled = YES;
    subtitleNode.placeholderColor = RGB_255(245, 245, 245);
    subtitleNode.layerBacked = YES;
    subtitleNode.maximumNumberOfLines = 1;
    NSDictionary *attrs = @{ NSFontAttributeName: [UIFont fontWithName:@"HelveticaNeue" size:15.0f] ,NSForegroundColorAttributeName: RGB_255(153,  153, 153)};
    NSString *test = @"测试";
    subtitleNode.attributedText = [[NSAttributedString alloc]initWithString:test.length > 11 ? [test substringToIndex:11] : test attributes:attrs];
    [self addSubnode:subtitleNode];
    _subtitleNode = subtitleNode;
}

- (void)addArrowNode
{
    ASImageNode *arrowNode = [[ASImageNode alloc]init];
    arrowNode.layerBacked = YES;
    arrowNode.backgroundColor = [UIColor clearColor];
    arrowNode.image = [UIImage imageNamed:@"xiala"];
    [self addSubnode:arrowNode];
    _arrowNode = arrowNode;
}

- (void)addLineNode
{
    ASImageNode *lineNode = [[ASImageNode alloc]init];
    lineNode.layerBacked = YES;
    lineNode.backgroundColor = RGB_255(230,230,230);
    [self addSubnode:lineNode];
    _lineNode = lineNode;
}

- (ASLayoutSpec *)layoutSpecThatFits:(ASSizeRange)constrainedSize
{
    _arrowNode.style.preferredSize = CGSizeMake(9, 15);
    _lineNode.style.preferredSize = CGSizeMake(constrainedSize.max.width, 0.5);

    ASStackLayoutSpec *iconLayout = [ASStackLayoutSpec stackLayoutSpecWithDirection:ASStackLayoutDirectionHorizontal spacing:15 justifyContent:ASStackLayoutJustifyContentStart alignItems:ASStackLayoutAlignItemsCenter children:@[_subtitleNode,_arrowNode]];
    
    ASStackLayoutSpec *a = [ASStackLayoutSpec stackLayoutSpecWithDirection:ASStackLayoutDirectionHorizontal spacing:10 justifyContent:ASStackLayoutJustifyContentSpaceBetween alignItems:ASStackLayoutAlignItemsCenter children:@[_titleNode,iconLayout]];
    
    ASInsetLayoutSpec *insetLayout = [ASInsetLayoutSpec insetLayoutSpecWithInsets:UIEdgeInsetsMake(15, 10, 15, 10) child:a];
    
    ASStackLayoutSpec *lineLayout = [ASStackLayoutSpec stackLayoutSpecWithDirection:ASStackLayoutDirectionVertical spacing:0 justifyContent:ASStackLayoutJustifyContentStart alignItems:ASStackLayoutAlignItemsStretch children:@[insetLayout,_lineNode]];
    
    return lineLayout;
}

@end
